package de.silvalauinger.objectclassmapper.schema.system.syntax;

import com.google.common.base.Optional;
import static com.google.common.base.Preconditions.checkNotNull;
import static de.silvalauinger.objectclassmapper.schema.DefaultSchemaManagerFactory.SCHEMA_MANAGER;
import java.util.IllegalFormatException;
import static org.apache.directory.shared.ldap.model.constants.SchemaConstants.*;
import org.apache.directory.shared.ldap.model.exception.LdapException;
import org.apache.directory.shared.ldap.model.schema.SyntaxChecker;

public final class ObjectIdentifier {

    //<editor-fold defaultstate="collapsed" desc="Statics">
    private final static SyntaxChecker SYNTAX_CHECKER = checkNotNull(SCHEMA_MANAGER.getSyntaxCheckerRegistry().get(OID_SYNTAX));

    public static Optional<ObjectIdentifier> tryCreate(final String objectIdentifier) {
        try {
            return Optional.of(new ObjectIdentifier(objectIdentifier));
        } catch (LdapException exception) {
            return Optional.absent();
        }
    }

    public static boolean isValidSyntax(final String objectIdentifier) {
        return SYNTAX_CHECKER.isValidSyntax(objectIdentifier);
    }
    //</editor-fold>    
    //<editor-fold defaultstate="collapsed" desc="Attributes">
    private final String objectIdentifier;
    //</editor-fold>

    //<editor-fold defaultstate="collapsed" desc="Constructor">
    public ObjectIdentifier(final String objectIdentifier) throws LdapException {
        SYNTAX_CHECKER.assertSyntax(objectIdentifier);
        this.objectIdentifier = objectIdentifier;
    }
    //</editor-fold>

    @Override
    public String toString() {
        return objectIdentifier;
    }
}